package com.cloud.bc.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.cloud.bc.model.entity.BcConfigs;
import com.cloud.bc.mapper.BcConfigsMapper;
import com.cloud.bc.model.enums.RedisKey;
import com.cloud.bc.service.BcConfigsService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.model.enums.RedisKeyEnum;
import com.cloud.redis.config.RedisUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * 系统配置
 *
 * @author douhaichao code generator
 * @date 2021-01-09 17:40:00
 */
@Service
@AllArgsConstructor
public class BcConfigsServiceImpl extends ServiceImpl<BcConfigsMapper, BcConfigs> implements BcConfigsService {
    @Autowired
    RedisUtils redisUtils;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcConfigs selectOne(BcConfigs req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcConfigs>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcConfigs> selectAll(BcConfigs req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcConfigs> selectPage(Page page, BcConfigs req) {
        IPage<BcConfigs> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcConfigs>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcConfigs> selectPageByLike(Page page, BcConfigs req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (!StrUtil.isEmpty(req.getName())) {
            queryWrapper.like("name", req.getName());
        }

        if (!StrUtil.isEmpty(req.getValue())) {
            queryWrapper.like("value", req.getValue());
        }

        if (!StrUtil.isEmpty(req.getTitle())) {
            queryWrapper.like("title", req.getTitle());
        }

        if (!StrUtil.isEmpty(req.getValType())) {
            queryWrapper.like("val_type", req.getValType());
        }

        if (!StrUtil.isEmpty(req.getItem())) {
            queryWrapper.like("item", req.getItem());
        }

        if (!StrUtil.isEmpty(req.getModule())) {
            queryWrapper.like("module", req.getModule());
        }

        if (null != req.getAutoload()) {
            queryWrapper.eq("autoload", req.getAutoload());
        }
        IPage<BcConfigs> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcConfigs req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public List<BcConfigs> selectCacheAll() {
        String redisKey = RedisKeyEnum.bcConfigRedisKey.getTitle();
        Object o = redisUtils.get(redisKey);
        List<BcConfigs> list = null;
        String data=Convert.toStr(o);
        if (StrUtil.isNotEmpty(data)) {
            return JSONUtil.toList(data, BcConfigs.class);
        }
        list = selectAll(null);
        String s = JSONUtil.toJsonStr(list);
        redisUtils.set(redisKey, s);
        return list;
    }

    @Override
    public String getValue(String key) {
        List<BcConfigs> list = selectCacheAll();
        if (null == list && list.size() <= 0) {
            return "";
        }
        BcConfigs bcConfigs = list.stream().filter(t -> t.getName().equals(key)).findFirst().orElse(new BcConfigs());
        return bcConfigs.getValue();
    }
}
